java IO

Posted by ZhengYang on 2016-09-20

文件读写常用的类

  • java.io.File
  • java.io.InputStream 抽象类
    • java.io.FileInputStream 参数为文件名
    • java.io.FilterInputStream
      • java.io.BufferedInputStream
      • java.io.DataInputStream
  • java.io.OutputStream 抽象类
    • java.io.FileOutputStream 参数为文件名
    • java.io.FilterOutputStream
      • java.io.BufferedOutputStream
      • java.io.DataOutputStream
  • java.io.Reader 抽象类
    • java.InputStreamReader 可以定义编码
      • java.io.FileReader
  • java.io.Writer 抽象类
    • java.io.OutputStreamWriter 可以定义编码
      • java.io.FileWriter

文件的创建和删除

  • delete方法只能删除普通文件,不能删除目录,即使是空白目录也不行。
  • File类不能访问文件内容,即不能够从文件中读取数据或往文件里写数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;
public class FileTest {
public static void main(String[] args) {
File f = new File("d:\\1.txt");
if(f.exists()){
f.delete(); // 删除文件
}else{
try{
f.createNewFile(); // 创建文件
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}

读(字节数组) 写(字节数组)

  • 只能对字节数组读写
  • 程序中一概使用unicode,文件中的中文使用的是gbk
  • 未使用缓冲区
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
public class FileStream {
public static void main(String[] args) {
File f = new File("d://1.txt");
// Output
try{
FileOutputStream out = new FileOutputStream(f);
byte[] buf = "0123456789abcdef".getBytes();
out.write(buf);
out.close();
}catch(Exception e){
System.out.print(e.getMessage());
}
// Input
try{
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.print(new String(buf,0,len));
}catch(Exception e){
System.out.print(e.getMessage());
}
}
}

读(字符数组) 写(字符串)

  • 读 改为操作字符数组,写 改为直接操作字符串
  • 注意这里是字符数组(char[]),不是字节数组(byte[])
  • 程序中一概使用unicode,文件中的中文可以使用的是gbk,utf-8
  • 使用了缓冲区

写入文件时

  1. 确定写入的文件的编码,gbk(asci),utf-8(无BOM)

读取文件时

  1. 确定文件的编码格式,gbk(asci),utf-8
  2. 如果是utf-8,还要确定有无BOM,有的话需要将\uFEFF替换掉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.*;
public class FileReaderWriter {
public static void main(String[] args) {
// Writer
try{
FileOutputStream f = new FileOutputStream("d://2.txt");
// gbk, "utf-8"(无BOM), "unicode"(big endian)
OutputStreamWriter out = new OutputStreamWriter(f, "utf-8");
out.write("abcedfghijklmnopqrstuvwxyz中国");
out.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
// Reader
try{
FileInputStream f = new FileInputStream("d://2.txt");
// gbk, "utf-8", "unicode"
InputStreamReader in = new InputStreamReader(f,"utf-8");
char[] buf = new char[1024]; // 这里是char,是Unicode编码
int len = in.read(buf);
in.close();
System.out.println(new String(buf,0,len)); // 无BOM的utf8
// 有BOM的utf8 0xEF0xBB0xBF
System.out.println(new String(buf,0,len).replace("\uFEFF", ""));
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

键盘输入

  • 使用Scanner类
  • 不定长输入,可以使用ArrayList类
  • ctrl + z 结束输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
public class Scan {
public static void main(String[] args) {
ArrayList<Integer> arrl = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
arrl.add(sc.nextInt());
}
sc.close();
for(int i=0; i<arrl.size(); ++i){
System.out.println(arrl.get(i));
}
}
}